package com.lane.sidecn.user.web;

import org.springframework.beans.BeanUtils;

import com.lane.core.service.BaseManager;
import com.lane.core.utils.MD5Util;
import com.lane.core.web.BaseController;
import com.lane.sidecn.user.model.Customer;

/**
 * 用户管理控制器
 * @author 黄连忠
 */
public class CustomerController extends BaseController {

	private BaseManager baseManager;

	private Customer customer;

	/**
	 * 新用户注册
	 */
	public void register() {
		String passord = MD5Util.crypt(customer.getPassword()); // 对密码进行加密
		customer.setPassword(passord);
		try {
			baseManager.save(customer);// 调用接口进行保存
			printMessage("registerSuccess"); //注册成功
			request.getSession().setAttribute("user", customer); // 将用户信息保存进session中
		} catch (Exception e) {
			printMessage("registerFail"); //注册失败
		} 
	}

	/**
	 * 登录
	 */
	public void login() {
		Customer tmpCustomer = baseManager.findUniqueBy(Customer.class, "email", customer.getEmail());
		if (null == customer) {
			printMessage("loginFail_Email"); //用户名不存在
		} else {
			/* 匹配密码 */
			String tmpPassword = MD5Util.crypt(tmpCustomer.getPassword());
			if (tmpCustomer.getPassword().equals(tmpPassword)) {
				printMessage("loginSuccess"); //登录成功
				request.getSession().setAttribute("user", customer); // 将用户信息保存进session中
			} else {
				printMessage("loginFail_Password"); //密码错误
			}
		}
	}
	
	/**
	 * 登出
	 */
	public void loginOut() {
		request.getSession().invalidate();
		printMessage("loginOut"); //登出
	}

	/**
	 * 修改密码
	 */
	public void modifyPassword() {
		String newPassword = request.getParameter("password");
		customer = (Customer) request.getSession().getAttribute("user");
		customer.setPassword(MD5Util.crypt(newPassword));
		try {
			baseManager.save(customer);
			printMessage("modifyPasswordSuccess"); // 密码修改成功
		} catch (Exception e) {
			printMessage("modifyPasswordFail"); // 密码修改失败
		}

	}

	/**
	 * 检测用户名是否已经被注册
	 */
	public void isEmailExist() {
		String email = request.getParameter("email");
		customer = baseManager.findUniqueBy(Customer.class, "email", email);
		if (null == customer){
			printMessage("emailNotExist"); // 用户不存在，没有被注册
		} else{
			printMessage("emailExist"); // 用户名存在，已经被注册
		}
	}

	/**
	 * 修改用户信息
	 */
	public void update() {
		Customer oldCustomer = baseManager.findUniqueBy(Customer.class, "email", customer.getEmail());
		String[] ignoreProperties = { "email", "password" };
		BeanUtils.copyProperties(customer, oldCustomer, ignoreProperties);
		BeanUtils.copyProperties(oldCustomer, customer);
		try {
			baseManager.save(customer);
			printMessage("updateInfoSuccess"); // 修改成功
			request.getSession().setAttribute("user", customer); // 更新session的信息
		} catch (Exception e) {
			printMessage("updateInfoFail"); // 修改失败
		}
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public void setBaseManager(BaseManager baseManager) {
		this.baseManager = baseManager;
	}
}
